package edu.gatech.oad.mule.view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import edu.gatech.oad.mule.model.GameStateMachine.GameState;
import edu.gatech.oad.mule.model.interfaces.GameStateMachineListener;
import edu.gatech.oad.mule.view.interfaces.RenderableGameDriver;

/**
 * This class represents one frame of the screen. It takes state information
 * from GameDriver and gathers the necessary Graphics objects, sprites, etc and
 * arranges them on the screen for displaying.
 * 
 * @author Brendan McGarry, Dale Avery, D'Miria Collins, Natasha Parekh, Jaemin
 *         Park
 * @version %I%, %G%
 */
public class View implements GameStateMachineListener {

	private static final int RESOLUTION_WIDTH = 600;
	private static final int RESOLUTION_HEIGHT = 600;

	private GameState gameState;
	// private GraphicsLibrary graphicsLib;
	Timer timer;
	private JFrame frame;
	private JPanel viewPanel;
	private RenderableGameDriver gameDriver;
	private IntroPanel intro;
	private PlayerRankingPanel ranking;
	private DevelopmentPanel map;
	private JPanel current;
	private PlayerInfoPanel infoPanel;
	private LandGrantPanel landGrant;
	private ProductionPanel production;

	/**
	 * This constructor configures a new View object
	 */
	public View(JFrame frame, RenderableGameDriver gameDriver) {
		this.frame = frame;
		this.gameDriver = gameDriver;
		// graphicsLib = new GraphicsLibrary();

		intro = new IntroPanel(new Dimension(RESOLUTION_WIDTH,
				RESOLUTION_HEIGHT));
		ranking = new PlayerRankingPanel(gameDriver, new Dimension(
				RESOLUTION_WIDTH, RESOLUTION_HEIGHT));
		map = new DevelopmentPanel(new Dimension(RESOLUTION_WIDTH,
				RESOLUTION_HEIGHT), gameDriver);
		landGrant = new LandGrantPanel(new Dimension(RESOLUTION_WIDTH,
				RESOLUTION_HEIGHT), gameDriver);
		production = new ProductionPanel(new Dimension(RESOLUTION_WIDTH,
				RESOLUTION_HEIGHT), gameDriver);

		//frame.add(intro);
		frame.pack();
		frame.setLocationRelativeTo(null);

		// Initialize timer for controlling turns
		timer = new Timer(50, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				draw();
			}
		});
		timer.start();

	}// end constructor

	@Override
	public void stateChange(GameState state) {
		this.gameState = state;

		if (current != null)
			frame.remove(current);

		if (state == GameState.INTRO) {
			frame.add(intro);
			current = intro;
		} else if (state == GameState.PLAYER_RANKING) {
			frame.add(ranking);
			ranking.update();
			current = ranking;
		} else if (state == GameState.LAND_GRANT) {
			// TODO Create a land grant panel. Using MapPanel for now.
			frame.add(landGrant);
			current = landGrant;
		} else if (state == GameState.DEVELOPMENT) {
			frame.add(map);
			current = map;
		} else if (state == GameState.PRODUCTION) {
			frame.add(production);
			current = production;
		} else {
			// TODO Create an error panel.
		}

		frame.pack();
		System.out.println("View register state change to: " + state);
	}

	private void draw() {
		frame.pack();
		frame.repaint();
	}

}// end class View

